#!/usr/bin/python

# Copyright 2015 by Platform Product Integration Team.
#
# Permission to use, copy, modify, and distribute this software and its
# documentation for any purpose and without fee is hereby granted,
# provided that the above copyright notice appear in all copies and that
# both that copyright notice and this permission notice appear in
# supporting documentation, not be used in advertising or publicity
# pertaining to distribution

import os,argparse,time

__author__ = "Youye Sun <youye.sun@emc.com>"
__version__ = filter(str.isdigit, "$Revision: 2892 $")
__date__ = "5/5/2015"

__filename__ = os.path.basename(__file__)

__timestart__ = time.strftime("%m-%d-%Y-%X")

__logfile__ = __filename__[: __filename__.find('.') if __filename__.find('.') != -1 else len(__filename__) + 1] + \
              "_log_" + __timestart__


def argument_parser():
    """
    Parse arguments if any
    """
    parser = argparse.ArgumentParser(prog=__filename__, usage='%(prog)s [options]')
    parser.add_argument('-tc', '--testconfig', help="provide configuration file for test cases", nargs='?',
                        const="testcase0.json", default="testcase0.json")
    parser.add_argument('--version', action='version', version='%(prog)s ' + __version__)
    parser.add_argument('-v', '--verbose', help="increase output verbosity", action="store_true")
    return parser.parse_args()


args = argument_parser()


class Logger:
    def __init__(self, name):
        """
        Initialize a logger handler.

        Create a log file handler with name and timestamp.
        """
        self._handler = open(name, 'w+')

    def __del__(self):
        """
        Close the file handler when logger is being destroyed.
        """
        self._handler.close()

    def write(self, *lines):
        """
        Write lines to the log file and print the line on the console.
        """
        for line in lines:
            self._handler.write(line + '\n')
            print line

    """
    Define write function for verbose mode.
    If verbose mode is not turned on, verbose write will be None.
    """
    if args.verbose:
        def verbosewrite(self, *lines):
            self.write(*lines)
    else:
        verbosewrite = lambda *a: None


log = Logger(__logfile__)


if __name__ == '__main__':
    print "See more details in log file: " + __logfile__
    log.write("Started at: " + __timestart__)
    if args.testconfig != "testcase0.json":
        log.verbosewrite("The configuration file for test cases: " + args.testconfig)
    else:
        log.verbosewrite("Use default configuration file for test cases: " + args.testconfig)





